package ex.hql.config;

import org.csource.fastdfs.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @program:yeb
 * @author: hql
 * @Time: 2021/9/16  16:33
 * @description: fastdfs工具类
 */
public class FastDFSUtils {

    private static final Logger logger= LoggerFactory.getLogger(FastDFSUtils.class);

    /**
     * 初始化客户端
     */
    static {
        try {
            ClientGlobal.initByProperties("fastdfs-client.properties");
        } catch (Exception e) {
            logger.error("初始化FastDfs失败",e.getMessage());
        }
    }

    /**
     * 生成TrackerServer服务器
     * @return
     */
    private static TrackerServer getTrackerServer() throws IOException {
        TrackerClient trackerClient=new TrackerClient();
        return trackerClient.getTrackerServer();
    }

    /**
     * 生成StorageClient
     * @return
     * @throws IOException
     */
    private static StorageClient getStorageClient() throws IOException {
        TrackerServer trackerServer=getTrackerServer();
        return new StorageClient(trackerServer,null);
    }


    /**
     * 上传文件
     * @param file
     * @return
     */
    public static String[] upload(MultipartFile file){
        String filename = file.getOriginalFilename();
        logger.info("文件名",filename);
        String[] upload_file=null;
        StorageClient storageClient=null;
        try {
            //获取 storageClient
            storageClient=getStorageClient();
          upload_file = storageClient.upload_file(file.getBytes(), filename.substring(filename.lastIndexOf(".") + 1), null);
        } catch (Exception e) {
            logger.error("上传文件失败",e.getMessage());
        }
        if(null==upload_file){
            logger.error("上传失败",storageClient.getErrorCode());
        }
        return upload_file;
    }

    /**
     * 获取文件信息
     * @param groupName
     * @param remoteFileName
     * @return
     */
    public static FileInfo getFileInfo(String groupName,String remoteFileName){
        StorageClient storageClient=null;
        try {
            storageClient=getStorageClient();
           return storageClient.get_file_info(groupName, remoteFileName);
        } catch (Exception e) {
           logger.error("文件信息获取失败",e.getMessage());
        }
        return null;
    }

    /**
     * 下载文件
     * @param groupName
     * @param remoteFileName
     * @return
     */
    public static InputStream downFile(String groupName,String remoteFileName){
        StorageClient storageClient=null;
        try {
            storageClient=getStorageClient();
            byte[] download_file = storageClient.download_file(groupName, remoteFileName);
         InputStream InputStream = new ByteArrayInputStream(download_file);
         return InputStream;
        } catch (Exception e) {
            logger.error("文件下载失败",e.getMessage());
        }
        return null;
    }

    /**
     * 删除文件
     * @param groupName
     * @param remoteFileName
     */
    public static void deleteFile(String groupName,String remoteFileName){
        StorageClient storageClient=null;
        try {
            storageClient=getStorageClient();
            storageClient.delete_file(groupName, remoteFileName);
        } catch (Exception e) {
           logger.error("删除文件失败",e.getMessage());
        }
    }


    public static String getTrackerUrl(){
        TrackerClient trackerClient=new TrackerClient();
        TrackerServer trackerServer=null;
        StorageServer storageServer=null;
        try {
            trackerServer = trackerClient.getTrackerServer();
            storageServer = trackerClient.getStoreStorage(trackerServer);
        } catch (Exception e) {
           logger.error("文件路径获取失败",e.getMessage());
        }
        return "http://"+storageServer.getInetSocketAddress().getHostString()+"/";
    }


}
